<?php

/**
 * This is the model class for table "sd.pages".
 *
 * The followings are the available columns in table 'sd.pages':
 * @property string $id
 * @property string $category_id
 * @property integer $gallery_id
 * @property string $user_add
 * @property string $user_edit
 * @property string $title
 * @property string $content
 * @property string $date_add
 * @property string $date_edit
 * @property integer $active
 * @property string $meta_keywords
 * @property string $meta_description
 * @property integer $set_form
 *
 * The followings are the available model relations:
 * @property Pages $parent
 * @property Pages[] $pages
 * @property Users $userAdd
 * @property Users $userEdit
 */
class Pages extends CBaseActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'sd.pages';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_add, title, description, date_add', 'required'),
			array('gallery_id, active, set_form, sort', 'numerical', 'integerOnly'=>true),
			array('category_id, user_add, user_edit, sort', 'length', 'max'=>10),
			array('title', 'length', 'max'=>200),
			array('type', 'length', 'max'=>45),
			array('meta_keywords, meta_description, alias', 'length', 'max'=>255),
			array('date_edit', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, alias, category_id, gallery_id, user_add, user_edit, title, description, date_add, date_edit, active, meta_keywords, meta_description, set_form, sort, type', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'pages' => array(self::HAS_MANY, 'Pages', 'parent_id'),
			'userAdd' => array(self::BELONGS_TO, 'Users', 'user_add'),
			'userEdit' => array(self::BELONGS_TO, 'Users', 'user_edit'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'alias' => 'Alias',
			'category_id' => 'Category',
			'gallery_id' => 'Gallery',
			'user_add' => 'User Add',
			'user_edit' => 'User Edit',
			'title' => 'Title',
			'description' => 'Content',
			'date_add' => 'Date Add',
			'date_edit' => 'Date Edit',
			'active' => 'Active',
			'meta_keywords' => 'Meta Keywords',
			'meta_description' => 'Meta Description',
			'set_form' => 'Set Form',
			'sort' => 'sort',
			'type' => 'type',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('alias',$this->alias,true);
		$criteria->compare('category_id',$this->category_id,true);
		$criteria->compare('gallery_id',$this->gallery_id);
		$criteria->compare('user_add',$this->user_add,true);
		$criteria->compare('user_edit',$this->user_edit,true);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('date_add',$this->date_add,true);
		$criteria->compare('date_edit',$this->date_edit,true);
		$criteria->compare('active',$this->active);
		$criteria->compare('meta_keywords',$this->meta_keywords,true);
		$criteria->compare('meta_description',$this->meta_description,true);
		$criteria->compare('set_form',$this->set_form);
		$criteria->compare('sort',$this->sort);
		$criteria->compare('type',$this->type);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Pages the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public function createUrlId()
	{
		return $this->alias;
	}

	public function getAll($oPager = null, $categoryId = null)
	{
		$oCriteria = $this->getDbCriteria();

		$oCriteria->order = 'id DESC';

		if($categoryId > 0)
			$oCriteria->addColumnCondition(array('category_id' => $categoryId));

		if($oPager)
		{
			$oPager->setItemCount($this->count($oCriteria));
			$oPager->applyLimit($oCriteria);
		}

		return $this->findAll($oCriteria);
	}

	public function selectByCategoryRowset($oTargetRowset)
	{
		if(!$oTargetRowset)
			return $oTargetRowset;

		$catIds = array();
		foreach($oTargetRowset as $oRow)
		{
			$catIds[$oRow->id] = $oRow->id;
		}

		$oCriteria = $this->getDbCriteria();
		$oCriteria->addInCondition('category_id', (array)$catIds);
		$oRowset = $this->findAll($oCriteria);

		$aRowset = array();
		foreach($oRowset as $oRow)
			$aRowset[$oRow->category_id][] = $oRow;

//		print_r($aRowset); exit;

		foreach($oTargetRowset as $key => $oTargetRow)
		{
			if(!empty($aRowset[$oTargetRow->id]))
			{
				$oTargetRowset[$key]['pages'] = $aRowset[$oTargetRow->id];
			}
		}

		return $oRowset;
	}

}
